// 云函数名称：review-topic
const db = uniCloud.database()
exports.main = async (event, context) => {
  try {
    const { topicId, action, reason } = event
    
    // 参数校验
    if (!topicId || !action) {
      return {
        code: 400,
        message: '缺少必要参数'
      }
    }

    // 定义更新内容
    const updateData = {
      type: 'pass',
      review_time: Date.now()
    }

    // 如果是拒绝操作添加原因
    if (action === 'reject') {
      if (!reason || reason.trim() === '') {
        return {
          code: 400,
          message: '拒绝原因不能为空'
        }
      }
      updateData.review_reason = reason
      updateData.type = 'reject' // 根据你的需求设置拒绝状态
    }

    // 执行数据库更新
    const result = await db.collection('topics')
      .doc(topicId)
      .update(updateData)

    if (result.updated === 1) {
      return {
        code: 200,
        message: '操作成功',
        data: result
      }
    }
    
    return {
      code: 404,
      message: '文档未找到'
    }
    
  } catch (e) {
    console.error('云函数错误:', e)
    return {
      code: 500,
      message: '服务端错误',
      error: e.message
    }
  }
}